//
//  DWJRetweetView.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/21.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit
import SnapKit

class DWJRetweetView: UIView {

    
    // 记录我们转发微博的底部约束
    var retweetViewConstraint: Constraint?
    
    var status: DWJStatus? {
    
        didSet {
        
            contentLabel.text = status?.retweeted_status?.text
            
            // 先要卸载我们的转发微博底部约束
            self.retweetViewConstraint?.uninstall()
            
            if status?.retweeted_status?.pic_urls?.count != 0 {
            
                pictureView.pictureInfo = status?.retweeted_status?.pic_urls
                
                // 有配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.hidden = false
                
            } else {
            
                // 没配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.hidden = true
                
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 设置视图
    private func setUpUI() {
        
        backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.left.equalTo(self).offset(10)
            make.width.equalTo(kWIDTH - 20)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        

    }
    
    // MARK: - 懒加载控件
    private lazy var contentLabel: UILabel = {
    
        let lab = UILabel(textColor: UIColor.blackColor(), textFont: 14)
        lab.numberOfLines = 0
        return lab
    }()
    
    // 转发微博配图
    private lazy var pictureView: DWJStatusPictureView = {
    
        let imageView = DWJStatusPictureView()
        
        return imageView
    }()

}
